﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Extensions.DependencyInjection;
using Om.Camera;

namespace OptoMeasure.Views
{
    /// <summary>
    /// CameraView.xaml 的交互逻辑
    /// </summary>
    public partial class CameraView : UserControl
    {
        private CameraViewModel _vm;
        private MainViewModel _mainVm;
        public CameraView()
        {
            InitializeComponent();

            this.Loaded+= (sender, args) =>
                _vm = (CameraViewModel)DataContext;

            _mainVm = App.Current.ServiceProvider.GetRequiredService<MainViewModel>();
        }

        private async void Image_OnMouseMove(object sender, MouseEventArgs e)
        {
            if(_vm?.Camera == null || !_vm.Camera.IsConnected)
                return;

            Point point = e.GetPosition(ImageCtrl);
            if (_vm.Camera is IrCamera)
            {
                var irCamera = (IrCamera)_vm.Camera;
                var temp = await irCamera.GetPointTemperature(point.X,point.Y);
                _vm.Info = $"坐标：({point.X},{point.Y})  温度：{temp.Avg}℃";
            }
        }

        private void ImageCtrl_OnMouseLeave(object sender, MouseEventArgs e)
        {
            _vm.Info = "";
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            _vm.IsFullScreenDisplay = true;
            _mainVm.CameraVmList.Remove(_vm);
            var window = new Window();
            window.Title = _vm.Title;
            window.Closed += Window_Closed;
            window.SizeChanged += WindowOnSizeChanged;

            window.Content = this;
            window.Show();
        }

        private void WindowOnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.Height = e.NewSize.Height - 40;
            this.Width = e.NewSize.Width - 15;
        }


        private void Window_Closed(object? sender, EventArgs e)
        {
            _vm.IsFullScreenDisplay = false;
            _mainVm.CameraVmList.Add(_vm);
        }
    }
}
